home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / zapem-0.000 / zapem-0 / zapem / player.cc < prev    next >
C/C++ Source or Header  |  1995-05-06  |  3KB  |  167 lines

  1. /*    Copyright Alex Hornby 1994/1995. All rights reserved.
  2.      See file README for details
  3. */
  4.  
  5. #include "sprite.h"
  6. #include "soul.h"
  7. #include "entity.h"
  8. #include "weapon.h"
  9. #include "control.h"
  10. #include "slots.h"
  11. #include "player.h"
  12.  
  13. #include <assert.h>
  14. #include <unistd.h>
  15. #include <vga.h>
  16. #include <vgakeyboard.h>
  17.  
  18. Player :: Player() : Entity(NUMLIVES,100,100)
  19. {
  20.     dir=STRAIGHT;
  21.     init();
  22. }
  23.  
  24. Player :: Player( const Player& p) : Entity(p)
  25. {
  26.     dir=p.dir;
  27.     init();
  28. }
  29.  
  30. Player& 
  31. Player :: operator=(const Player& p)
  32. {
  33.     Entity::operator=(p);
  34.     return *this;
  35. }
  36.  
  37. void
  38. Player :: init(void)
  39. {
  40.     /* Set up the animations */
  41.     animExp=animElt(EXPLODESLOT,3)+ animElt(EXPLODESLOT+1,3)+
  42.         animElt(EXPLODESLOT+2,1)+animElt(SHIPSLOT,1);
  43.         animLeft=animElt(SHIPSLOT+2,5)+animElt(SHIPSLOT+3,5)+
  44.         animElt(LOOPANIM,0);
  45.         animRight=animElt(SHIPSLOT+4,5)+animElt(SHIPSLOT+5,5)+
  46.         animElt(LOOPANIM,0);
  47.         animStr=animElt(SHIPSLOT,5)+animElt(SHIPSLOT+1,5)+
  48.         animElt(LOOPANIM,0);
  49.  
  50.     /* We have selected no controller yet */
  51.     controller=0;
  52.     for(int i=0;i<=STARBOARD;i++)
  53.         weapon[i]=0;
  54. }
  55.  
  56. void
  57. Player :: setControl( Control* c)
  58. {
  59.     controller=c;
  60. }
  61.  
  62. void
  63. Player :: setWeapon( Weapon* w, rail r)
  64. {
  65.     weapon[r]=w;
  66. }
  67.  
  68. void
  69. Player :: explode()
  70. {
  71.     loadFilm(animExp);
  72.     setAnim(true);
  73. }
  74.  
  75. void
  76. Player :: fire()
  77. {
  78.     for(int i=0;i<4;i++)
  79.     {
  80.         if(weapon[i]!=0)
  81.             {
  82.             weapon[i]->fire();
  83.             }
  84.     }
  85. }
  86.  
  87. bool
  88. Player :: hit( const Sprite& e)
  89. {
  90.     for(int i=0;i<4;i++)
  91.     {
  92.         if(weapon[i]!=0 && weapon[i]->collide(e))
  93.             return true;
  94.     }
  95.     return false;
  96. }
  97.  
  98. void
  99. Player :: moveWeapons(void)
  100. {    
  101.     for(int i=0;i<4;i++)
  102.     {
  103.         if(weapon[i]!=0)
  104.             weapon[i]->move();
  105.     }
  106. }
  107.  
  108. void
  109. Player :: control(void)
  110. {
  111.     assert(controller!=0);
  112.  
  113.     /* Read controller */
  114.     controller->fetch();
  115.     
  116.     if(dir==UP && getAnim()==true)
  117.         return ;
  118.  
  119.     /* Act on the data read */
  120.     if( controller->test(RIGHT) ) 
  121.     {
  122.         move(1,0);
  123.         if(getDir()!=RIGHT)
  124.         {
  125.             setAnim(true);
  126.             loadFilm(animRight);
  127.             setDir(RIGHT);
  128.         }
  129.     }    
  130.  
  131.     else if( controller->test(LEFT) ) 
  132.     {
  133.         move(-1,0); 
  134.         if(getDir()!=LEFT)
  135.         {
  136.             setAnim(true);
  137.             loadFilm(animLeft);
  138.             setDir(LEFT);
  139.         }
  140.     }    
  141.     else if (getDir()!=STRAIGHT)
  142.     {
  143.         setAnim(true);
  144.         loadFilm(animStr);
  145.         setDir(STRAIGHT);
  146.     }
  147.  
  148.     if( controller->test(FIRE1) ) fire();
  149.     if( controller->test(QUIT) ) setLives(0);
  150.     if( controller->test(PAUSE) ) 
  151.     {
  152.         if(controller->getRaw())
  153.             keyboard_close();
  154.         while(vga_getkey()!='p')
  155.             usleep(10000);
  156.         if(controller->getRaw())
  157.             keyboard_init();
  158.     }
  159.     /* Keep object on screen */
  160.     if(xp>getScreenWidth()-getWidth()) 
  161.         xp=getScreenWidth()-getWidth();
  162.     if(xp<0) xp=0;
  163.  
  164.     /* Also control its objects( ie Weapons) */
  165.     moveWeapons();
  166. }
  167.